package com.xiaoxin.yixinai.companyversion;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.xiaoxin.yixinai._frame.base.BaseModel;

/**
 * 公司版本类
 * @author lilipo
 *
 */
@Entity
@Table(name = "tb_company_version")
public class CompanyVersion extends BaseModel {

    /**
     * 主键
     */
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    
    /**
     * 公司主键
     */
    @Column(name = "company_id")
    private Integer companyId;
    
    /**
     * 版本主键
     */
    @Column(name = "version_id")
    private Integer versionId;
    
    /**
     * 开始时间
     */
    @Column(name = "begin_time")
    private Date beginTime;
    
    /**
     * 结束时间（保存数据库时，若只传年月日时，则把时分秒设为23:59:59）
     */
    @Column(name = "end_time")
    private Date endTime;
    
    /**
     * 激活（0否，1是）
     */
    @Column(name = "activated")
    private Integer activated;
    
    /**
     * 时长（单位：月）
     */
    @Column(name = "duration_month")
    private Integer durationMonth;
    
    /**
     * 时长（单位：天）
     */
    @Column(name = "duration_day")
    private Integer durationDay;
    
	
	/**
	 * 剩余天数（结束时间 - 当前时间）
	 */
	@Transient
	private Integer remainingDays;
    
    /**
     * 计算结束时间
     */
    public void computingEndTime() {
    	if (this.beginTime == null) {
    		return ;
    	}
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(this.beginTime);
    	
    	if (this.durationMonth != null) {
    		cal.add(Calendar.MONTH, this.durationMonth);
    	}
    	if (this.durationDay != null) {
    		cal.add(Calendar.DAY_OF_YEAR, this.durationDay);
    	}
    	this.endTime = cal.getTime();
    }

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}

	public Integer getVersionId() {
		return versionId;
	}

	public void setVersionId(Integer versionId) {
		this.versionId = versionId;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Integer getActivated() {
		return activated;
	}

	public void setActivated(Integer activated) {
		this.activated = activated;
	}

	public Integer getDurationMonth() {
		return durationMonth;
	}

	public void setDurationMonth(Integer durationMonth) {
		this.durationMonth = durationMonth;
	}

	public Integer getDurationDay() {
		return durationDay;
	}

	public void setDurationDay(Integer durationDay) {
		this.durationDay = durationDay;
	}

	/**
	 * 计算剩余天数，若未激活或者错误数据（没有结束时间），则返回null
	 * @return Integer 剩余天数
	 */
	public Integer getRemainingDays() {
		if (this.activated == null || this.endTime == null) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		long now = cal.getTime().getTime();
		
		cal.setTime(this.endTime);
		if (cal.get(Calendar.HOUR_OF_DAY) != 23) {
			cal.set(Calendar.HOUR_OF_DAY, 23);
		}
		if (cal.get(Calendar.MINUTE) != 59) {
			cal.set(Calendar.MINUTE, 59);
		}
		if (cal.get(Calendar.SECOND) != 59) {
			cal.set(Calendar.SECOND, 59);
		}
		if (cal.get(Calendar.MILLISECOND) != 999) {
			cal.set(Calendar.MILLISECOND, 999);
		}
		long newEndTime = cal.getTime().getTime();
		
		// 结束时间 - 当前时间
		if ( (newEndTime - now) % (1000 * 60 * 60 * 24) == 0 ) {
			this.remainingDays = (int) ((newEndTime - now) / (1000 * 60 * 60 * 24));
		} else {
			this.remainingDays = (int) ((newEndTime - now) / (1000 * 60 * 60 * 24)) + 1;
		}
		return remainingDays.intValue() < 0 ? 0 : remainingDays;
	}
    
}
